<template>
  <!--  面包屑-->
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="row-play">
      用户等级：
      <el-select  class="row-play-p" v-model="rankValue" placeholder="请选择">
        <el-option
            v-for="item in rankList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      用户状态：
      <el-select class="row-play-p" v-model="statusValue" placeholder="请选择">
        <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      用户名：
      <el-input class="row-play-play" v-model="userName" placeholder="请输入内容"></el-input>
      <el-button class="row-play-p" @click="getPage">搜索</el-button>
      <el-button class="row-play-p" @click="toggleSelection()">取消选择</el-button>
      <el-button @click="deleteMany" type="danger">批量删除</el-button>
    </div>
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="修改时间"
          width="180">
        <template slot-scope="scope">{{ scope.row.updateTime }}</template>
      </el-table-column>
      <el-table-column
          label="用户名"
          width="120">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>性别:
              <template v-if="scope.row.sex === 1">
                男
              </template>
              <template v-else-if="scope.row.sex === 0">
                女
              </template>
              <template v-else>
                {{ scope.row.sex }}
              </template>
            </p>
            <p>邮箱: {{ scope.row.email }}</p>
            <p>手机号: {{ scope.row.phone }}</p>
            <p>创建时间: {{ scope.row.createTime }}</p>
            <p>修改人: {{ scope.row.updateUserName }}</p>
            <p>修改时间: {{ scope.row.updateTime }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.userName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
          label="昵称"
          width="120">
        <template slot-scope="scope">{{ scope.row.nickName }}</template>
      </el-table-column>
      <el-table-column
          label="级别"
          width="100">
        <template slot-scope="scope">
          <template v-if="scope.row.rank === 1">
            管理员
          </template>
          <template v-else-if="scope.row.rank === 2">
            普通用户
          </template>
          <template v-else>
            {{ scope.row.rank }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
          label="状态"
          width="100">
        <template slot-scope="scope">
          <template v-if="scope.row.status === 1">
            在线
          </template>
          <template v-else-if="scope.row.status === 0">
            冻结
          </template>
          <template v-else>
            {{ scope.row.status }}
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="等级" :label-width="formLabelWidth">
          <el-select v-model="editForm.rank" placeholder="请选择活动区域">
            <el-option label="管理员" :value="1"></el-option>
            <el-option label="用户" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="editForm.status" placeholder="请选择活动区域">
            <el-option label="在线" :value="1"></el-option>
            <el-option label="冻结" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    <div style="margin-top: 20px">-->
    <!--      <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>-->
    <!--    </div>-->
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize">
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      rankList: [{
        value: 1,
        label: '管理员'
      }, {
        value: 2,
        label: '用户'
      }, {
        value: null,
        label: ''
      }],
      statusList: [{
        value: 0,
        label: '冻结'
      }, {
        value: 1,
        label: '在线'
      }, {
        value: null,
        label: ''
      }],
      rankValue: null,
      statusValue: null,
      userName: '',
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dialogFormVisible: false,
      editForm: {
        id:null,
        rank: null,
        status: null,
      },
      formLabelWidth: '120px'
    }
  },
  // created() {
  //   this.getPage();
  // },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(row) {
      // console.log(row);
      this.editForm.id=row.id;
      this.editForm.status=row.status;
      this.editForm.rank=row.rank;
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      // console.log(index, row);
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {
        const {data: res} = await this.$http.delete("/employee/" + row.id);
        if (res.code === 0) {
          return this.$message.error(res.msg);
        }
        await this.getPage();
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getPage();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getPage();
    },
    async getPage() {
      const {data: res} = await this.$http.get("/employee/page", {
        params: {
          page: this.currentPage,
          pageSize: this.pageSize,
          rank: this.rankValue,
          status: this.statusValue,
          userName: this.userName
        }
      });
      if (res.code === 0) {
        return this.$message.error("获取用户信息失败");
      }
      this.tableData = res.data.records;
      this.totalSize = res.data.total;
    },
    async edit(){
      this.dialogFormVisible = false;
      const {data: res} = await this.$http.put("/employee/edit",this.editForm);
      if (res.code === 0) {
        return this.$message.error("修改用户信息失败");
      }
      this.$message({
        type: 'success',
        message: '修改成功!'
      });
      await this.getPage();
    },
    cancelEdit(){
      this.dialogFormVisible = false;
      this.$message({
        type: 'info',
        message: '取消编辑'
      });
    },
    deleteMany(){
      let ids=[];
      this.multipleSelection.forEach(row=>{
        ids.push(row.id);
      })
      let idss=ids.join(',');
      this.$confirm('此操作将永久删除多个用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {
        const {data: res} = await this.$http.delete("/employee",{
          params:{
            ids:idss
          }
        });
        if (res.code === 0) {
          return this.$message.error(res.msg);
        }
        await this.getPage();
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.getPage();
  }
}
</script>

<style scoped lang="less">
.row-play {
  display: flex;
  align-items: center;

  .row-play-play {
    width: 200px;
    margin-right: 20px;
  }

  .row-play-p {
    margin-right: 20px;
  }
}

.block {
  text-align: center;
}
</style>